﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell.Commands;

namespace Nivot.PowerShell.WindowsMobile.Commands
{
    [Cmdlet(VerbsData.ConvertFrom, "WMFile", DefaultParameterSetName = "Path", SupportsShouldProcess = true)]
    public class ConvertFromWMFileCommand : WMPathCommandBase
    {
        protected override void ProcessRecord()
        {
            ProviderInfo provider;
            PSDriveInfo driveInfo;

            string destinationPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(this.Destination, out provider, out driveInfo);
            
            if (provider.ImplementingType == typeof(FileSystemProvider))
            {
                bool isContainer = this.InvokeProvider.Item.IsContainer(this.Destination);
                if (!isContainer)
                {
                    this.ThrowTerminatingError(
                        new ErrorRecord(new IOException("Destination should be a directory."),
                                        "InvalidDestination", ErrorCategory.InvalidArgument, this.Destination));
                }
            }
            else
            {
                this.ThrowTerminatingError(
                    new ErrorRecord(new IOException("Destination should be in the FileSystemProvider."),
                                    "InvalidProvider", ErrorCategory.InvalidArgument, this.Destination));
            }

            bool shouldGlob = (this.ParameterSetName == ParamSetPath);

            string[] sourcePaths = shouldGlob ? this.Path : this.LiteralPath;            

            foreach (string sourcePath in sourcePaths)
            {
                if (shouldGlob)
                {
                    WriteVerbose(String.Format("Globbing {0}", sourcePath));
                    Collection<PathInfo> pathInfos = this.SessionState.Path.GetResolvedPSPathFromPSPath(sourcePath);
                    foreach (PathInfo pathInfo in pathInfos)
                    {
                        string devicePath = pathInfo.ProviderPath;
                        CopyFromDevice(devicePath, destinationPath);
                    }
                }
                else
                {
                    string devicePath = sourcePath;
                    if (ShouldProcess(String.Format("{0} => {1}", devicePath, destinationPath), "ConvertFrom"))
                    {
                        CopyFromDevice(devicePath, destinationPath);
                    }
                } 
            }
        }

        private void CopyFromDevice(string devicePath, string destinationPath)
        {
            destinationPath = this.SessionState.Path.Combine(destinationPath,
                this.SessionState.Path.ParseChildName(devicePath));

            if (ShouldProcess(String.Format("{0} => {1}", devicePath, destinationPath), "ConvertFrom"))
            {
                Device.CopyFileFromDevice(devicePath, destinationPath, this.Force);
            }
        }
    }
}
